package com.netease.financial.base.pdf;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.netease.financial.R;
import com.netease.financial.common.util.NfsLog;
import com.netease.financial.common.util.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CloudPdfActivity extends Activity implements CloudPdfView {

    private static final String TAG = CloudPdfActivity.class.getSimpleName();

    private static final String INTENT_EXTRA_PARAM_PDF_URL = "pdf_url";
    private CloudPdfPresenter mCloudPdfPresenter;

    @Bind(R.id.rl_progress)
    RelativeLayout rl_progress;

    public static Intent getCallingIntent(Context context, String pdfUrl) {
        Intent callingIntent = new Intent(context, CloudPdfActivity.class);
        callingIntent.putExtra(INTENT_EXTRA_PARAM_PDF_URL, pdfUrl);
        return callingIntent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_pdf);

        ButterKnife.bind(this);
        String pdfUrl = getIntent().getStringExtra(INTENT_EXTRA_PARAM_PDF_URL);
        if (TextUtils.isEmpty(pdfUrl)) {
            NfsLog.d(TAG, "invalid pdf url");
            finish();
            return;
        }
        mCloudPdfPresenter = new CloudPdfPresenterImpl();
        mCloudPdfPresenter.setView(this);
        mCloudPdfPresenter.onCreate();
        mCloudPdfPresenter.requestPdf(pdfUrl);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        mCloudPdfPresenter.onDestroy();
    }

    @Override
    public void renderPdf(final Uri pdfUri) {
        Intent intent = new Intent(this, PdfActivity.class);
        intent.setData(pdfUri);
        startActivity(intent);
        finish();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(this, message);
        finish();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading() {
        rl_progress.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progress.setVisibility(View.INVISIBLE);
    }

}
